package com.agile.leetcode.hot.easy;

/**
 * 位运算计算两位是的和
 *
 * @Author:ChenZhangKun
 * @Date: 2021/4/30 14:11
 */
public class CalculateSumWithBit {
    public static void main(String[] args) {
        CalculateSumWithBit calculateSumWithBit = new CalculateSumWithBit();
        int result = calculateSumWithBit.add(10, 20);
        System.out.println(result);
    }

    public int add(int a, int b) {
        // 当进位为0跳出时
        while (b != 0) {
            // 进位
            int c = (a & b) << 1;
            // a=非进位和
            a ^= b;
            // b=进位
            b = c;
        }
        return a;
    }
}
